home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / HEXDUMP.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  93 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  This Program Written By Paul Edwards.                            */
  4. /*  Dedicated to the public domain.                                  */
  5. /*                                                                   */
  6. /*********************************************************************/
  7. /*********************************************************************/
  8. /*                                                                   */
  9. /*  hexdump - dump a file.                                           */
  10. /*                                                                   */
  11. /*********************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17.  
  18. static void dodump(FILE *fp, int count);
  19. static void skipb(FILE *fp, int start);
  20.  
  21. main(int argc, char **argv)
  22. {
  23.       FILE *fp;
  24.       int start, count;
  25.  
  26.       if (argc < 2)
  27.       {
  28.             printf("hexdump - must pass name of file to dump\n");
  29.             return (EXIT_FAILURE);
  30.       }
  31.       if (argc > 2)
  32.             start = atoi(*(argv+2));
  33.       else  start = -1;
  34.       if (argc > 3)
  35.             count = atoi(*(argv+3));
  36.       else  count = -1;
  37.       fp = fopen(*(argv+1),"rb");
  38.       if (fp == NULL)
  39.       {
  40.             printf("unable to open file %s for input\n",*(argv+1));
  41.             return (EXIT_FAILURE);
  42.       }
  43.       skipb(fp,start);
  44.       dodump(fp,count);
  45.       return (EXIT_SUCCESS);
  46. }
  47.  
  48. static void dodump(FILE *fp, int count)
  49. {
  50.       int c,x=0,pos1,pos2;
  51.       char prtln[100];
  52.  
  53.       while (((c = fgetc(fp)) != EOF) && (x != count))
  54.       {
  55.             if (x%16 == 0)
  56.             {
  57.                   memset(prtln,' ',sizeof prtln);
  58.                   sprintf(prtln,"%0.6X   ",x);
  59.                   pos1 = 8;
  60.                   pos2 = 45;
  61.             }
  62.             sprintf(prtln+pos1,"%0.2X",c);
  63.             if (isprint(c))
  64.                   sprintf(prtln+pos2,"%c",c);
  65.             else  sprintf(prtln+pos2,".");
  66.             pos1+=2;
  67.             *(prtln+pos1) = ' ';
  68.             pos2++;
  69.             if (x%4 == 3)
  70.                   *(prtln+pos1++) = ' ';
  71.             if (x%16 == 15)
  72.                   printf("%s\n",prtln);
  73.             x++;
  74.       }
  75.       if (x%16 != 15)
  76.             printf("%s\n",prtln);
  77.       return;
  78. }
  79.  
  80. static void skipb(FILE *fp, int start)
  81. {
  82.       int x=0;
  83.  
  84.       if (start < 0)
  85.             return;
  86.       while (x < start)
  87.       {
  88.             fgetc(fp);
  89.             x++;
  90.       }
  91.       return;
  92. }
  93.